package com.tianyu.inspection.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tianyu.inspection.common.enums.ClientSoftwareType;
import com.tianyu.inspection.common.model.PageModel;
import com.tianyu.inspection.service.api.IClientSoftwareService;
import io.ocs.model.OcsSimpleModel;
import com.tianyu.inspection.common.component.AppConstants;
import com.tianyu.inspection.entity.ClientSoftware;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.mapper.ClientSoftwareMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description 客户端软件服务
 * @Date 2021/2/23 - 15:19
 **********************************/

@Slf4j
@Service
public class ClientSoftwareServiceImpl implements IClientSoftwareService {

    @Resource
    private ClientSoftwareMapper clientSoftwareMapper;

    @Resource
    private OcsSimpleModel ocsSimpleModel;

    @Resource
    private AppConstants appConstants;

    /**
     * 分页查询
     *
     * @param queryMo 参数模型
     * @return 分页记录对象
     */
    @Override
    public IPage<ClientSoftware> searchListPage(QueryMo queryMo) {
        IPage<ClientSoftware> page = PageModel.getModelPage(queryMo, clientSoftwareMapper);
        page.getRecords().forEach(clientSoftware -> {
            if (Objects.nonNull(clientSoftware.getUrl())) {
                clientSoftware.setUrl(ocsSimpleModel.getObjectUrl(clientSoftware.getUrl()));
                clientSoftware.setFullUrl(ocsSimpleModel.getObjectUrl(clientSoftware.getFullUrl()));
            }
        });
        return page;
    }

    /**
     * 通过ID等参数查询一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 单条记录结果
     */
    @Override
    public ClientSoftware selectOne(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id")) || Objects.nonNull(queryMo.getEqMap().get("version_number"))) {
            final ClientSoftware clientSoftware = clientSoftwareMapper.selectOne(new QueryWrapper<ClientSoftware>().allEq(queryMo.getEqMap()));
            if (Objects.nonNull(clientSoftware) && Objects.nonNull(clientSoftware.getUrl())) {
                clientSoftware.setUrl(ocsSimpleModel.getObjectUrl(clientSoftware.getUrl()));
                clientSoftware.setFullUrl(ocsSimpleModel.getObjectUrl(clientSoftware.getFullUrl()));
            }
            return clientSoftware;
        }
        return null;
    }

    /**
     * 插入一条数据
     *
     * @param operateMo 实体对象
     * @return 是否操作成功
     */
    @Override
    public boolean insert(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ClientSoftware clientSoftware = objectMapper.convertValue(operateMo.getEntity(), ClientSoftware.class);
        if (Objects.nonNull(clientSoftware) && Objects.nonNull(clientSoftware.getFullUrl())) {
            if (clientSoftware.getType().equals(ClientSoftwareType.WIN_PROD_EXE.getValue())) {
                ocsSimpleModel.copyObject(clientSoftware.getFullUrl().substring(1), appConstants.winClientPath);
            }
            if (clientSoftware.getType().equals(ClientSoftwareType.ANDROID_PROD_APP.getValue())) {
                ocsSimpleModel.copyObject(clientSoftware.getFullUrl().substring(1), appConstants.androidClientPath);
            }
        }
        return 1 == clientSoftwareMapper.insert(clientSoftware);
    }

    /**
     * 通过ID删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean delete(QueryMo queryMo) {
        if (Objects.nonNull(queryMo.getEqMap().get("id"))) {
            return 1 == clientSoftwareMapper.delete(new QueryWrapper<ClientSoftware>().allEq(queryMo.getEqMap()));
        }
        return false;
    }

    /**
     * 通过ID更新一条数据
     *
     * @param operateMo 实体对象参数集
     * @return 是否操作成功
     */
    @Override
    public boolean update(OperateMo operateMo) {
        ObjectMapper objectMapper = new ObjectMapper();
        ClientSoftware clientSoftware = objectMapper.convertValue(operateMo.getEntity(), ClientSoftware.class);
        operateMo.getEqMap().remove("project_id");
        if (Objects.nonNull(clientSoftware) && Objects.nonNull(clientSoftware.getFullUrl())) {
            if (clientSoftware.getType().equals(ClientSoftwareType.WIN_PROD_EXE.getValue())) {
                final ClientSoftware clientSoftware0 = clientSoftwareMapper.selectOne(
                        new QueryWrapper<ClientSoftware>().orderByDesc("id").eq("type", ClientSoftwareType.WIN_PROD_EXE.getValue()).last("limit 1"));
                if (Objects.nonNull(clientSoftware0) && clientSoftware0.getId().equals(operateMo.getEqMap().get("id"))) {
                    ocsSimpleModel.copyObject(clientSoftware.getFullUrl().substring(1), appConstants.winClientPath);
                } else {
                    return false;
                }
            } else if (clientSoftware.getType().equals(ClientSoftwareType.ANDROID_PROD_APP.getValue())) {
                final ClientSoftware clientSoftware0 = clientSoftwareMapper.selectOne(
                        new QueryWrapper<ClientSoftware>().orderByDesc("id").eq("type", ClientSoftwareType.ANDROID_PROD_APP.getValue()).last("limit 1"));
                if (Objects.nonNull(clientSoftware0) && clientSoftware0.getId().equals(operateMo.getEqMap().get("id"))) {
                    ocsSimpleModel.copyObject(clientSoftware.getFullUrl().substring(1), appConstants.androidClientPath);
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return 1 == clientSoftwareMapper.update(clientSoftware, new QueryWrapper<ClientSoftware>().allEq(operateMo.getEqMap()));
    }

    /**
     * 通过ID逻辑删除一条数据
     *
     * @param queryMo 参数集[实体ID 与 项目ID, 数据状态等]
     * @return 是否操作成功
     */
    @Override
    public boolean logicallyDelete(QueryMo queryMo) {
        return 1 == clientSoftwareMapper.update(
                new ClientSoftware().setState(1),
                new QueryWrapper<ClientSoftware>().allEq(queryMo.getEqMap())
        );
    }

    /**
     * 获取最新版本
     *
     * @return
     */
    @Override
    public ClientSoftware getLatestVersion(QueryMo queryMo) {
        queryMo.setPageNum(0);
        queryMo.setDescParams("create_time");
        queryMo.build(ClientSoftware.class, null);
        IPage<ClientSoftware> page = searchListPage(queryMo);
        if (page.getRecords().size() > 0) {
            return page.getRecords().get(0);
        }
        return null;
    }

    /**
     * 是否是最新版本
     *
     * @return
     */
    @Override
    public Boolean isLatestVersion(QueryMo queryMo) {
        ClientSoftware latestVersion = getLatestVersion(queryMo);
        ClientSoftware clientSoftware = selectOne(queryMo);
        if (Objects.nonNull(latestVersion) && Objects.nonNull(clientSoftware)) {
            return latestVersion.getId().equals(clientSoftware.getId());
        }
        return false;
    }
}
